package cn.edu.scau.cmi.chenjiale.spring;

import org.springframework.context.ApplicationContext;

import cn.edu.scau.cmi.chenjiale.spring.entity.Student;
import cn.edu.scau.cmi.chenjiale.spring.entity.Teacher;
import cn.edu.scau.cmi.chenjiale.spring.util.ApplicationContextUtil;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class SpringClient extends Application {

	private ApplicationContext applicationContext;

	@Override
	public void start(Stage primaryStage) throws Exception {
		HBox btnPane = new HBox(10);
		BorderPane pane = new BorderPane();

		Button generateBtn = new Button("获取bean");
		btnPane.getChildren().addAll(generateBtn);

		TextArea contentArea = new TextArea();
		pane.setTop(btnPane);
		pane.setCenter(contentArea);

		Scene scene = new Scene(pane, 300, 300);
		primaryStage.setScene(scene);
		primaryStage.setTitle("spring");
		primaryStage.show();

		generateBtn.setOnAction(e -> {
			applicationContext = ApplicationContextUtil.getApplicationContext();
			Student student = applicationContext.getBean(Student.class);
			Teacher teacher = student.getTutor();
			contentArea.setText("学生姓名:" + student.getName() + "\n教师姓名:" + teacher.getName());
		});
	}

	public static void main(String[] args) {
		Application.launch(args);
	}

}
